//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.SqlCe;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;

using System.Xml;
using System.Xml.Serialization;

using System.IO;

using Shp.Runtime.Contract;
using System.Threading;
using System.Collections.ObjectModel;

namespace LitwareHR.PublicApp.WinUX.Services
{
    public class OfflineDataService
    {
        private DBService dbService = null;

        [InjectionConstructor]
        public OfflineDataService([ServiceDependency] DBService dbService)
        {
            this.dbService = dbService;            
        }

        /// <summary>
        /// Updates local store with fresh information 
        /// </summary>
        /// <param name="entityName">Name of the entity to persist</param>
        /// <param name="keyName">Name of the key</param>
        /// <param name="entityInstanceDef">Entity definition and values</param>
        public void UpdateEntityTable(string entityName, string keyName, StringPair[][] entityInstanceDef)
        {
            dbService.UpdateEntityTable(entityName, keyName, entityInstanceDef);
        }

        public int DeleteClosedPositions(string entityName, string keyName, Collection<Guid> openPositionList)
        {
            return dbService.DeleteClosedPosition(entityName, keyName, openPositionList);
        }

        public DataTable GetOpenPositions()
        {
            string entityName = Constants.Entity.Position;
            return dbService.GetAllEntities(entityName);
        }

        public DataTable GetOpenPosition(string positionId)
        {
            string entityName = Constants.Entity.Position;
            string entityKeyName = "id";  
            return dbService.GetEntities(entityName, entityKeyName, positionId);
        }

        public DataTable GetRecruitingWorkFlowList()
        {
            string entityName = "RecruitingWorkFlowList";
            return dbService.GetAllEntities(entityName);            
        }

        public DataTable GetRecruitingWorkflowListByApplicant()
        {
            string applicant = string.Format("'{0}@{1}'", Properties.Settings.Default.UserName, Properties.Settings.Default.TenantAlias);
            string entityName = "RecruitingWorkFlowList";
            string entityKeyName = "[Applicant]";
            return dbService.GetEntities(entityName, entityKeyName, applicant);
        }

        public void AddInstance(string entityName, EntityField[] list)
        {
            dbService.AddInstance(entityName, list);
        }

        public List<Guid> GetOpenPositionIds()
        {
            return dbService.GetOpenPositionIds();
        }

        /// <summary>
        /// Returns the Definition of an Entity stored in the local DB
        /// </summary>
        /// <param name="entityName">Name of the Entity</param>
        /// <returns>A list of EntityFields</returns>
        public EntityField[] GetEntityDef(string entityName)
        {
            EntityField[] returnDef = null;
            XmlSerializer serializer = new XmlSerializer(typeof(EntityField[]));
            string entityDef = dbService.GetEntityDef(entityName);
            if (entityDef == null)
                return returnDef;
            StringReader reader = new StringReader(entityDef);
            returnDef = (EntityField[])serializer.Deserialize(reader);
            return returnDef;
        }

        public string GetSerializedEntityDef(string entityName)
        {
            return dbService.GetEntityDef(entityName); 
        }

        public void CreateEntityDef(string entityName, string currentEntityDefStr, EntityField[] entityDef)
        {
            dbService.CreateEntityDef(entityName, currentEntityDefStr, entityDef);
        }

        public void AlterEntityDef(EntityField[] localEntityDef, EntityField[] currentEntityDef, string entityName, string currentEntityDefStr)
        {
            dbService.AlterEntityDef(localEntityDef, currentEntityDef, entityName, currentEntityDefStr);  
        }

        public void CleanLocalDB()
        {
            dbService.CleanLocalDB();  
        }
    }
}
